//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
namespace DotNetNuke.Security.Permissions
{
	public class TabPermissionController
	{
		private static PermissionProvider provider = PermissionProvider.Instance();
		private static void ClearPermissionCache(int tabId)
		{
			TabController objTabs = new TabController();
			TabInfo objTab = objTabs.GetTab(tabId, Null.NullInteger, false);
			DataCache.ClearTabPermissionsCache(objTab.PortalID);
		}
		public static bool CanAddContentToPage()
		{
			return CanAddContentToPage(TabController.CurrentPage);
		}
		public static bool CanAddContentToPage(TabInfo objTab)
		{
			return provider.CanAddContentToPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanAddPage()
		{
			return CanAddPage(TabController.CurrentPage);
		}
		public static bool CanAddPage(TabInfo objTab)
		{
			return provider.CanAddPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanAdminPage()
		{
			return CanAdminPage(TabController.CurrentPage);
		}
		public static bool CanAdminPage(TabInfo objTab)
		{
			return provider.CanAdminPage(objTab);
		}
		public static bool CanCopyPage()
		{
			return CanCopyPage(TabController.CurrentPage);
		}
		public static bool CanCopyPage(TabInfo objTab)
		{
			return provider.CanCopyPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanDeletePage()
		{
			return CanDeletePage(TabController.CurrentPage);
		}
		public static bool CanDeletePage(TabInfo objTab)
		{
			return provider.CanDeletePage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanExportPage()
		{
			return CanExportPage(TabController.CurrentPage);
		}
		public static bool CanExportPage(TabInfo objTab)
		{
			return provider.CanExportPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanImportPage()
		{
			return CanImportPage(TabController.CurrentPage);
		}
		public static bool CanImportPage(TabInfo objTab)
		{
			return provider.CanImportPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanManagePage()
		{
			return CanManagePage(TabController.CurrentPage);
		}
		public static bool CanManagePage(TabInfo objTab)
		{
			return provider.CanManagePage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanNavigateToPage()
		{
			return CanNavigateToPage(TabController.CurrentPage);
		}
		public static bool CanNavigateToPage(TabInfo objTab)
		{
			return provider.CanNavigateToPage(objTab) || CanAdminPage(objTab);
		}
		public static bool CanViewPage()
		{
			return CanViewPage(TabController.CurrentPage);
		}
		public static bool CanViewPage(TabInfo objTab)
		{
			return provider.CanViewPage(objTab) || CanAdminPage(objTab);
		}
		public static void DeleteTabPermissionsByUser(UserInfo objUser)
		{
			provider.DeleteTabPermissionsByUser(objUser);
			Services.Log.EventLog.EventLogController objEventLog = new Services.Log.EventLog.EventLogController();
			objEventLog.AddLog(objUser, PortalController.GetCurrentPortalSettings(), UserController.GetCurrentUserInfo().UserID, "", DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.TABPERMISSION_DELETED);
			DataCache.ClearTabPermissionsCache(objUser.PortalID);
		}
		public static TabPermissionCollection GetTabPermissions(int tabID, int portalID)
		{
			return provider.GetTabPermissions(tabID, portalID);
		}
		public static bool HasTabPermission(string permissionKey)
		{
			return HasTabPermission(PortalController.GetCurrentPortalSettings().ActiveTab.TabPermissions, permissionKey);
		}
		public static bool HasTabPermission(TabPermissionCollection objTabPermissions, string permissionKey)
		{
			bool hasPermission = provider.HasTabPermission(objTabPermissions, "EDIT");
			if (!hasPermission) {
				if (permissionKey.Contains(",")) {
					foreach (string permission in permissionKey.Split(',')) {
						if (provider.HasTabPermission(objTabPermissions, permission)) {
							hasPermission = true;
							break;
						}
					}
				} else {
					hasPermission = provider.HasTabPermission(objTabPermissions, permissionKey);
				}
			}
			return hasPermission;
		}
		public static void SaveTabPermissions(TabInfo objTab)
		{
			provider.SaveTabPermissions(objTab);
			Services.Log.EventLog.EventLogController objEventLog = new Services.Log.EventLog.EventLogController();
			objEventLog.AddLog(objTab, PortalController.GetCurrentPortalSettings(), UserController.GetCurrentUserInfo().UserID, "", DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.TABPERMISSION_UPDATED);
			ClearPermissionCache(objTab.TabID);
		}
		
	}
}
